//
//  HomeViewController.swift
//  DLPhoto
//
//  Created by mac1012 on 2017/11/1.
//  Copyright © 2017年 DianlE. All rights reserved.
//

import UIKit
import SnapKit

class HomeViewController: UIViewController {

    fileprivate let imagePicker = UIImagePickerController()
    
    
    
    
    let backgourndImageView:UIImageView = {
        ()->UIImageView in
        let imageView = UIImageView(frame: CGRect.init(x: 0, y: 0, width: ScreenUtils.screenWidth(), height: ScreenUtils.screenHeight()))
        imageView.image = UIImage(named: "pic")
        return imageView
    }()
    
    fileprivate let albumBtn:SNButton = {
        ()->SNButton in
        let orignX:CGFloat = 33/2
        let orignY:CGFloat = SNUtils.screenSize.height - 80
        let snBtn = SNButton(frame: CGRect(x: orignX, y: orignY, width: 45, height: 38), withImageNamed: "galleryButton")
        return snBtn
    }()
    
    
    fileprivate let camerarBtn = {
        ()->SNButton in
        let orignX:CGFloat =  33/2 + 45 + 8
        let orignY:CGFloat = SNUtils.screenSize.height - 80
        let snBtn = SNButton(frame: CGRect(x: orignX, y: orignY, width: 45, height: 38), withImageNamed: "camerar")
        return snBtn
    }()
    
    fileprivate let timeNodeBtn = {
        ()->SNButton in
        let orignX:CGFloat =  ScreenUtils.screenWidth() -  45  - 33/2
        let orignY:CGFloat = SNUtils.screenSize.height - 80
        let snBtn = SNButton(frame: CGRect(x: orignX, y: orignY, width: 45, height: 35), withImageNamed: "timenode")
        return snBtn
    }()
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(backgourndImageView)
        self.view.addSubview(albumBtn)
        self.view.addSubview(camerarBtn)
        self.view.addSubview(timeNodeBtn)
        self.setupEvent()
        NotificationCenter.default.addObserver(self, selector: #selector(observerNotification(info:)), name: NSNotification.Name(rawValue: "takePhoto"), object: nil)
        AdvertisingManager.share().showPop(self, with: Banner) {
            
        }
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = true
    }
    
    func observerNotification(info:NSNotification){
        let image = info.object as! UIImage
        let editVC = EditPhotoViewController()
        editVC.editImage = image
        
        self.navigationController?.pushViewController(editVC, animated: false)
    }
    
    func setupEvent(){
        self.albumBtn.setAction {
            [weak weakSelf = self] in
            weakSelf?.openAlbum()
        }
        
        self.camerarBtn.setAction {
            [weak weakSelf = self] in
            let camerarVC = CamerarViewController()
            weakSelf?.navigationController?.pushViewController(camerarVC, animated: true)
        }
        
        self.timeNodeBtn.setAction {
            [weak weakSelf = self] in
            let camerarList = CamerarListViewController()
            weakSelf?.navigationController?.pushViewController(camerarList, animated: false)
            
        }
        
        //        let leftSWipe = UISwipeGestureRecognizer.init(target: self, action: #selector(leftSwipe(swipeGesture:)))
        //        leftSWipe.direction = .left
        //        self.view.addGestureRecognizer(leftSWipe)
    }
    
    func leftSwipe(swipeGesture:UIGestureRecognizer){
        let camerarList = CamerarListViewController()
        self.navigationController?.pushViewController(camerarList, animated: false)
    }
    
    
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension HomeViewController:UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    func openAlbum() -> Void {
        
        let pickerVC = UIImagePickerController()
        pickerVC.sourceType = .photoLibrary
        pickerVC.delegate = self
        pickerVC.allowsEditing = true
        present(pickerVC, animated: false, completion: nil)
        
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let image:UIImage? = info[UIImagePickerControllerEditedImage] as? UIImage
        if image != nil {
            let editVC = EditPhotoViewController()
            editVC.editImage = image
            
            self.navigationController?.pushViewController(editVC, animated: false)
        }
        picker.dismiss(animated: true, completion: nil)
    }
    
}
